package pe.edu.upc.studio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import pe.edu.upc.studio.excepcion.DAOExcepcion;
import pe.edu.upc.studio.modelo.Sala;
import pe.edu.upc.studio.modelo.Articulo;
import pe.edu.upc.studio.modelo.GestorSala;
import pe.edu.upc.studio.modelo.Reserva;
import pe.edu.upc.studio.util.ConexionBD;

public class ArmadoSalaDAO extends BaseDAO {
	
	public Collection<GestorSala> listarSalaArmado(String fecha, int idArmado) throws DAOExcepcion {
		System.out.println("listarSalaArmado()");
		Collection<GestorSala> lista = new ArrayList<GestorSala>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "Select a.idSala, b.Nombre, b.Local, b.Capacidad, b.Caracteristica," +
							"a.idArticulo,c.Tipo,a.CostoxHora,a.CantArticulo, a.CostoxHora * a.CantArticulo As Importe,d.Fecha " +
							"From GestorSala a " +
							"Inner Join Sala b On a.idSala = b.idSala " +
							"Inner Join Articulo c On a.idArticulo = c.idArticulo " +
							"Left Join Reserva d On a.idSala = d.idSala " +
							"Where d.Fecha <> '" + fecha + "'" +
							"And a.idArmado=" + idArmado;
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				GestorSala objGestorSala = new GestorSala();
				Sala objSala = new Sala();
				Articulo objArticulo = new Articulo();
				//objGestorSala.setIdArmado(rs.getInt("idArmado"));
				objSala.setIdSala(rs.getInt("idSala"));
				objGestorSala.setIdSala(objSala);
				objArticulo.setIdArticulo(rs.getInt("idArticulo"));
				objGestorSala.setIdArticulo(objArticulo);
				objGestorSala.setCostoHora(rs.getDouble("CostoxHora"));
				objGestorSala.setCantArticulo(rs.getInt("CantArticulo"));
				lista.add(objGestorSala);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

}
